package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/*
* 权限服务
* */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;


    //角色管理查询角色权限
    public Set<Permission> findAll() {
        return permissionDao.findAll();
    }

    //权限管理分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();//获得当前页
        Integer pageSize = queryPageBean.getPageSize();//获得每页展示数据条数
        String queryString = queryPageBean.getQueryString();//获取查询条件(用户名)

        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //权限管理-编辑权限前回显数据的请求路径
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    //编辑权限
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public void delete(Integer id) {
        permissionDao.deleteRoleIdByPermission(id);
        permissionDao.delete(id);
    }

    @Override
    public Permission findPermissionByKeyword(String keyword) {
        return permissionDao.findPermissionByKeyword(keyword);
    }
}
